//https://www.nowcoder.com/practice/6ab1d9a29e88450685099d45c9e31e46?tpId=13&tqId=23257&ru=/exam/oj/ta&qru=/ta/coding-interviews/question-ranking&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D1%26tpId%3D13%26type%3D13

/*
struct ListNode {
	int val;
	struct ListNode *next;
	ListNode(int x) :
			val(x), next(NULL) {
	}
};*/

//本来想写递归但没过
class Solution {
public:
    ListNode* FindFirstCommonNode( ListNode* pHead1, ListNode* pHead2) {
        
		// if(pHead1 == nullptr)
		// {
		// 	return pHead2;
		// }
		// if(pHead2 == nullptr)
		// {
		// 	return pHead1;
		// }

		// while(pHead1->val == pHead2->val)
		// {
		// 	return pHead1;
		// }

		// ListNode* cur = FindFirstCommonNode(pHead1->next, pHead2->next);

		// return cur; 

        //使节点长度相等 a + b = b + a
		ListNode* ta = pHead1;
		ListNode* tb = pHead2;

		while(ta != tb)
		{
			ta = ta ? ta->next : pHead2;
			tb = tb ? tb->next : pHead1;
		}

		return ta;

    }
};
